import { ref } from "vue";
import { storageLocal } from "/@/utils/storage";
import NotifMessage from "/@/utils/message/notification";
import { useRouter } from "vue-router";
import { download } from "/@/utils/download";

// const router = useRouter();

const FileTypes = {
  doc: "application/msword",
  docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  xls: "application/vnd.ms-excel",
  xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  pdf: "application/pdf",
  ppt: "application/vnd.ms-powerpoint",
  pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  png: "image/png",
  jpg: "image/jpeg",
  jpeg: "image/jpeg"
};

export default function (): any {
  const dataList = ref([]);
  const acceptTypes = Object.values(FileTypes).join(",");

  const uploadUrl =
    import.meta.env.VITE_BASE_URL + "/rdms-admin/common/file/upload";

  const uploadComUrl =
    import.meta.env.VITE_BASE_URL + "/rdms-admin/common/file/upload";

  const uploadHeaders = {
    Authorization: storageLocal.getItem("rdms-info")?.accessToken
  };

  const handleError = err => {
    const router = useRouter();

    let myError = err.toString(); //转字符串
    myError = myError.replace("Error: ", ""); // 去掉前面的" Error: "
    myError = JSON.parse(myError); //转对象
    myError?.message && NotifMessage.error(myError?.message);

    if (myError?.body?.code && [401, 403].includes(myError?.body?.code)) {
      myError?.body?.message && NotifMessage.error(myError?.body?.message);
      router.push("/login");
      storageLocal.removeItem("rdms-info");
    }
  };

  const beforeAvatarUpload = file => {
    const type = file.name.substring(file.name.lastIndexOf(".") + 1);
    if (!Object.keys(FileTypes).includes(type)) {
      NotifMessage.warning(`请上传${Object.keys(FileTypes).join("、")}等格式!`);
      return false;
    }

    const isLt2M = file.size / 1024 / 1024 < 10;

    if (!isLt2M) {
      NotifMessage.warning(`文件大小不能超过10M!`);
      return false;
    }
  };

  const handlePreview = file => {
    console.log(file, "file");
    if (file?.response?.code !== 200 || !file?.response?.data) return;
    download(file?.response?.data);
  };

  const handleDownload = file => {
    download(file);
  };

  const onFilterFileData = (list: any[]) => {
    if (!list || !list.length) return;
    const newFileData = list.map(item => {
      // if (item.status === "success") {
      return item.response.data;
      // }
    });
    return newFileData;
  };

  const onInitFileList = (list: any[]) => {
    if (!list || !list.length || !list[0]?.fileName) return;
    const newFileList = list.map(item => {
      return {
        name: item?.fileName,
        response: {
          data: item,
          code: 200
        }
      };
    });
    return newFileList;
  };

  return {
    dataList,
    acceptTypes,
    uploadUrl,
    uploadComUrl,
    uploadHeaders,
    handleError,
    beforeAvatarUpload,
    handlePreview,
    handleDownload,
    onFilterFileData,
    onInitFileList
  };
}
